package com.dingwen.treasure.model.vo;

import cn.hutool.core.collection.CollUtil;
import com.dingwen.treasure.base.constant.ManageConstant;
import com.dingwen.treasure.model.entity.BaseTree;
import com.dingwen.treasure.model.entity.mybatisplus.SysElement;
import com.dingwen.treasure.model.entity.mybatisplus.SysMenu;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 　SysMenuVO
 * 　@author dingwen
 * 　@date 2022/6/21
 */
@ApiModel(value = "sysMenuVO", description = "系统菜单视图对象")
@Accessors(chain = true)
@Data
@AllArgsConstructor
@NoArgsConstructor
public class SysMenuVo implements Serializable, BaseTree {

    private static final long serialVersionUID = -7264708598159424649L;
    /**
     * 菜单名称
     */
    @ApiModelProperty(value = "菜单名称")
    private String name;

    /**
     * 组件名称
     */
    @ApiModelProperty(value = "组件名称")
    private String component;

    /**
     * 组件路径
     */
    @ApiModelProperty(value = "组件路径")
    private String path;

    /**
     * 元信息
     */
    @ApiModelProperty(value = "元信息")
    private Meta meta;
    /**
     * 主键
     */
    @JsonIgnore
    private Long id;

    /**
     * 父节点ID
     */
    @JsonIgnore
    private Long parenId;


    /**
     * 孩子节点集合
     */
    @ApiModelProperty(value = "子菜单信息")
    private List children;


    /**
     * 设置子项(数据中转)
     *
     * @param children 儿童
     */
    @Override
    public void setChildren(List children) {
        this.children = children;
    }

    /**
     * 获取子项(数据中转)
     *
     * @return {@link List}
     */
    @Override
    public List getChildren() {
        return children;
    }

    /**
     * 获取当前元素ID
     *
     * @return {@link Long}
     */
    @Override
    public Long getId() {
        return id;
    }

    /**
     * 获取树参数id
     *
     * @return {@link Long}
     */
    @Override
    public Long getParenId() {
        return parenId;
    }


    /**
     * 元
     *
     * @author dingwen
     * @date 2022/06/22
     */
    @ApiModel(value = "Meta", description = "元素信息")
    @Data
    @Builder
    @AllArgsConstructor
    @NoArgsConstructor
    public static class Meta {

        /**
         * 元素标题
         */
        @ApiModelProperty(value = "元素标题")
        private String title;

        /**
         * 元素权限信息
         */
        @ApiModelProperty(value = "元素权限信息")
        private String[] elementPermissions = new String[0];

        /**
         * 元素图标
         */
        @ApiModelProperty(value = "元素图标")
        private String icon;
    }


    /**
     * 次构造方法用于提供构建属性菜单
     *
     * @param sysMenu
     */
    public SysMenuVo(SysMenu sysMenu) {
        BeanUtils.copyProperties(sysMenu, this);
        // 元素权限信息
        List<SysElement> elements = sysMenu.getElements();
        if (CollUtil.isEmpty(elements)) {
            return;
        }
        List<String> elementsPermissions = elements
                .parallelStream()
                .map(ele -> ManageConstant.ELEMENT_PERMS_PREFIX.concat(ele.getPerms()))
                .collect(Collectors.toList());
        String[] elementPermissions = new String[elementsPermissions.size()];

        Meta meta = Meta.builder().title(sysMenu.getTitle())
                .icon(sysMenu.getIcon())
                .elementPermissions(elementsPermissions.toArray(elementPermissions))
                .build();
        this.meta = meta;
        this.children = sysMenu.getChildren();
    }
}
